from CTPTraderType import *
import globalvariable
global dick_tick
dick_tick = {}
#连续3比Tick上涨买入
#连续3比Tick下跌卖出
def OnTick(marketdata, strategyname):
    print(__name__)
    global dick_tick

    if marketdata.contents.InstrumentID in dick_tick:
        print("aaa: "+str(marketdata.contents.InstrumentID, encoding="utf-8"))
        pass
    else:
        list_tick = []
        dick_tick[marketdata.contents.InstrumentID] = list_tick
        print("bbb: "+str(marketdata.contents.InstrumentID, encoding="utf-8"))

    dick_tick[marketdata.contents.InstrumentID].insert(0, marketdata.contents.LastPrice)

    if len(dick_tick[marketdata.contents.InstrumentID])>3:
        print('运行策略('+strategyname+'.py)： '+str(marketdata.contents.InstrumentID, encoding="utf-8")+ ',' +str(marketdata.contents.LastPrice))
        print("比较" + str(marketdata.contents.InstrumentID, encoding="utf-8") + ',' +str(dick_tick[marketdata.contents.InstrumentID][0]) + ','+
        str(dick_tick[marketdata.contents.InstrumentID][1]) + ','+ str(dick_tick[marketdata.contents.InstrumentID][2]))
        if dick_tick[marketdata.contents.InstrumentID][2]>1e-7 and dick_tick[marketdata.contents.InstrumentID][0]>dick_tick[marketdata.contents.InstrumentID][1] and dick_tick[marketdata.contents.InstrumentID][1]>dick_tick[marketdata.contents.InstrumentID][2]:
            print("买入"+str(marketdata.contents.InstrumentID, encoding="utf-8"))
            #策略池功能完善中
            #用限价单 VN_OPT_LimitPrice 方式报单 。通常用涨停价限价单模拟市价，因为市价指令不支持所有的交易所
            globalvariable.td.InsertOrder(marketdata.contents.InstrumentID, '0', '0',VN_OPT_LimitPrice , marketdata.contents.highestprice, 1)
        elif dick_tick[marketdata.contents.InstrumentID][2]>1e-7 and dick_tick[marketdata.contents.InstrumentID][0]<dick_tick[marketdata.contents.InstrumentID][1] and dick_tick[marketdata.contents.InstrumentID][1]<dick_tick[marketdata.contents.InstrumentID][2]:
            print("卖出"+str(marketdata.contents.InstrumentID, encoding="utf-8"))
            # 策略池功能完善中
            #用限价单 VN_OPT_LimitPrice 方式报单 。通常用涨停价限价单模拟市价，因为市价指令不支持所有的交易所
            globalvariable.td.InsertOrder(marketdata.contents.InstrumentID, '1', '1',VN_OPT_LimitPrice , marketdata.contents.lowestprice, 1)

    print( globalvariable.get_value('key1') + globalvariable.get_value('key2'))
